package com.lovepick.database;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.lovepick.GlobalApp;
import com.lovepick.bean.Order;


public class DatabaseHelper extends SQLiteOpenHelper
{
	private final static int VERSION = 1;
	public final static String DB_NAME = "lovepick.db";
	private static final String TAG = "DatabaseHelper";

	private final String CREATE_MSGLIST_TABLE_SQL =
			"CREATE TABLE IF NOT EXISTS "
			+ MsgListTable.TABLE_NAME
			+ " ("
			+ MsgListTable.USER_ID + " INTEGER PRIMARY KEY, "
			+ MsgListTable.NICKNAME + " TEXT, "
			+ MsgListTable.LAST_MSG + " TEXT, "
			+ MsgListTable.LAST_DATE + " TEXT, "
			+ MsgListTable.LAST_ID + " INTEGER, "
			+ MsgListTable.HAS_NEW + " INTEGER"
			+ ");";

	private final String CREATE_FRIEND_TABLE_SQL =
			"CREATE TABLE IF NOT EXISTS "
			+ FriendTable.TABLE_NAME
			+ " ("
			+ FriendTable.USER_ID + " INTEGER PRIMARY KEY, "
			+ FriendTable.NICKNAME + " TEXT, "
			+ FriendTable.AVATAR + " INTEGER, "
			+ FriendTable.STATUS + " TEXT"
			+ ");";

	private final String CREATE_ORDER_DRIVER_TABLE_SQL =
			"CREATE TABLE IF NOT EXISTS "
			+ Order.ORDER_DRIVER_TABLE
			+ " ("
			+ Order.ORDER_ID + " INTERGER PRIMARY KEY, "
			+ Order.NICKNAME + " TEXT, "
			+ Order.GOTIME + " TEXT, "
			+ Order.SRC + " TEXT, "
			+ Order.SLAT + " REAL, "
			+ Order.SLNG + " REAL, "
			+ Order.DEST + " TEXT, "
			+ Order.DLAT + " REAL, "
			+ Order.DLNG + " REAL, "
			+ Order.MONEY + " INTEGER, "
			+ Order.STATE + " TEXT, "
			+ Order.OFFSET + " INTEGER, "
			+ Order.COUNT + " INTEGER, "
			+ Order.POINTS + " TEXT, "
			+ Order.MEM_ID + " INTEGER"
			+ ");";

	private final String CREATE_ORDER_PASSENGER_TABLE_SQL =
			"CREATE TABLE IF NOT EXISTS "
			+ Order.ORDER_PASSENGER_TABLE
			+ " ("
			+ Order.ORDER_ID + " INTERGER PRIMARY KEY, "
			+ Order.NICKNAME + " TEXT, "
			+ Order.GOTIME + " TEXT, "
			+ Order.SRC + " TEXT, "
			+ Order.SLAT + " REAL, "
			+ Order.SLNG + " REAL, "
			+ Order.DEST + " TEXT, "
			+ Order.DLAT + " REAL, "
			+ Order.DLNG + " REAL, "
			+ Order.MONEY + " INTEGER, "
			+ Order.STATE + " TEXT, "
			+ Order.OFFSET + " INTEGER, "
			+ Order.COUNT + " INTEGER, "
			+ Order.POINTS + " TEXT, "
			+ Order.MEM_ID + " INTEGER"
			+ ");";

	private final String DELETE_MSGLIST_TABLE_SQL =
			"DROP TABLE IF EXISTS " + MsgListTable.TABLE_NAME;
	private final String DELETE_FRIEND_TABLE_SQL =
			"DROP TABLE IF EXISTS " + FriendTable.TABLE_NAME;
	private final String DELETE_ORDER_DRIVER_TABLE_SQL =
			"DROP TABLE IF EXISTS " + Order.ORDER_DRIVER_TABLE;
	private final String DELETE_ORDER_PASSENGER_TABLE_SQL =
			"DROP TABLE IF EXISTS " + Order.ORDER_PASSENGER_TABLE;

	public DatabaseHelper()
	{
		super(GlobalApp.getInstance(), DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		// TODO Auto-generated method stub
		Log.i(TAG, "-----create database-----");
		Log.i(TAG, CREATE_MSGLIST_TABLE_SQL);
		Log.i(TAG, CREATE_FRIEND_TABLE_SQL);
		db.execSQL(CREATE_MSGLIST_TABLE_SQL);
		db.execSQL(CREATE_FRIEND_TABLE_SQL);
		db.execSQL(CREATE_ORDER_DRIVER_TABLE_SQL);
		db.execSQL(CREATE_ORDER_PASSENGER_TABLE_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub
		Log.i(TAG, "-----upgrade database-----");
		//Log.i(TAG, DELETE_MSGLIST_TABLE_SQL);
		db.execSQL(DELETE_MSGLIST_TABLE_SQL);
		db.execSQL(DELETE_FRIEND_TABLE_SQL);
		db.execSQL(DELETE_ORDER_DRIVER_TABLE_SQL);
		db.execSQL(DELETE_ORDER_PASSENGER_TABLE_SQL);
		onCreate(db);
	}

}
